package com.example.mapp.buildSql;

import com.example.entity.check.SelfCheck;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

/**
 * Created by simon on 2018/5/29.
 */
public class SelfCheckMappSql {

    public String queryList(SelfCheck selfCheck) {
        String select = " a.* ";
        String table = " self_check a ";
        String whereParam = "";
        if (null != selfCheck.getElevatorId() && !selfCheck.getElevatorId().equals("")) {
            whereParam += " and a.elevator_id = #{elevatorId}";
        }

        String where = " 1=1 " + whereParam;
        return new SQL() {
            {
                SELECT(select);
                FROM(table);
                WHERE(where);
                ORDER_BY(" a.check_time desc ");
            }
        }.toString();
    }

    /**
     * 批量插入年度自检检查结果
     */
    public String insertBatch(Map map){
        List<String> list = (List<String>) map.get("list");
        StringBuilder builder = new StringBuilder();
        builder.append("INSERT INTO self_check_result (check_item_id, check_item_name, check_result, self_check_id) VALUES ");
        for(int i = 0,len = list.size(); i < len; i++){
            builder.append("(");
            builder.append("#{list[").append(i).append("].checkItemId},");
            builder.append("#{list[").append(i).append("].checkItemName},");
            builder.append("#{list[").append(i).append("].checkResult},");
            builder.append("#{list[").append(i).append("].selfCheckId}");
            builder.append(")");

            if(i != len - 1){
                builder.append(",");
            }
        }
        System.out.println(builder.toString());
        return builder.toString();
    }

    /**
     * 获取检查项结果
     * @param selfCheck
     * @return
     */
    public String queryCheckResult(SelfCheck selfCheck){
        StringBuilder builder = new StringBuilder("select B.item_key as itemKey,A.check_result as itemValue from self_check_result A " +
                "LEFT JOIN check_item B ON A.check_item_id = B.id where 1=1 ");
        if(selfCheck.getId() != null){
            builder.append("and self_check_id = #{id}");
        }
        return builder.toString();
    }
}
